package com.fast.linkbeanadmin.utils.redis;

import cn.hutool.json.JSONUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author ruan cai yuan
 * @version 1.0
 * @fileName com.fast.linkbeanadmin.utils.redis.RedisUtil
 * @description: redis工具类
 * @since 2024/8/28 09:24
 */
@Component
public class RedisUtil {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public <T> void set(String key, T t, long timeout, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(t), timeout, timeUnit);
    }

    public <T> T get(String key, Class<T> clazz) {
        Object obj = redisTemplate.opsForValue().get(key);
        if (obj == null) {
            return null;
        }
        return JSONUtil.toBean(obj.toString(), clazz);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }
}
